﻿using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.Assertions;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.IMGUI.Controls;

namespace Hotfix
{
    /// <summary>
    /// 
    /// </summary>
    public class XHotfixAssetDatabaseDataSource : XHotfixAssetBundleDataSource
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<XHotfixAssetBundleDataSource> CreateDataSources()
        {
            var retList = new List<XHotfixAssetBundleDataSource> {
                new XHotfixAssetDatabaseDataSource()
            };

            return retList;
        }

        /// <summary>
        /// 
        /// </summary>
        public string Name 
        {
            get {
                return "Default";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ProviderName {
            get {
                return "Built-in";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool CanSpecifyBuildTarget
        {
            get { return true; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool CanSpecifyBuildOutputDirectory
        {
            get { return true; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool CanSpecifyBuildOptions
        {
            get { return true; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetBundleName"></param>
        /// <returns></returns>
        public string[] GetAssetPathsFromAssetBundle (string assetBundleName) 
        {
            return AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetPath"></param>
        /// <returns></returns>
        public string GetAssetBundleName(string assetPath) 
        {
            var importer = AssetImporter.GetAtPath(assetPath);
            if (!importer) 
            {
                return string.Empty;
            }

            var bundleName = importer.assetBundleName;
            if (importer.assetBundleVariant.Length > 0) {
                bundleName = bundleName + "." + importer.assetBundleVariant;
            }
            return bundleName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetPath"></param>
        /// <returns></returns>
        public string GetImplicitAssetBundleName(string assetPath)
        {
            return AssetDatabase.GetImplicitAssetBundleName (assetPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string[] GetAllAssetBundleNames() 
        {
            return AssetDatabase.GetAllAssetBundleNames ();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsReadOnly() 
        {
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetPath"></param>
        /// <param name="bundleName"></param>
        /// <param name="variantName"></param>
        public void SetAssetBundleNameAndVariant (string assetPath, string bundleName, string variantName)
        {
            AssetImporter.GetAtPath(assetPath).SetAssetBundleNameAndVariant(bundleName, variantName);
        }

        /// <summary>
        /// 
        /// </summary>
        public void RemoveUnusedAssetBundleNames() 
        {
            AssetDatabase.RemoveUnusedAssetBundleNames ();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool BuildAssetBundles (XHotfixAssetBundleBuildInfo info) {
            if(info == null)
            {
                Debug.Log("Error in build");
                return false;
            }

            var buildManifest = BuildPipeline.BuildAssetBundles(info.outputDirectory, info.options, info.buildTarget);
            if (buildManifest == null)
            {
                Debug.Log("Error in build");
                return false;
            }

            foreach(var assetBundleName in buildManifest.GetAllAssetBundles())
            {
                if (info.onBuild != null)
                {
                    info.onBuild(assetBundleName);
                }
            }

            return true;
        }
    }
}
